package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/27 6:29 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1247 {

    public int minimumSwap(String s1, String s2) {

        // s1 x x
        // s2 y y
        // 只需要交换一次
        //
        // s1 x y
        // s2 x y
        // 只需要交换两次

        // how many pairs we have for xy yx
        // res = xy/2 + yx/2
        // mod1 = xy%2
        // mod2 = yx%2

        // if (mod1 == mod2)
        // result += mod1*2
        // else
        // return -1

        int length = s1.length();
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();
        int countXy = 0;
        int countYx = 0;
        for (int i = 0; i < length; i++) {
            if (chars1[i] == 'x' && chars2[i] == 'y') {
                countXy++;
            }
            if (chars1[i] == 'y' && chars2[i] == 'x') {
                countYx++;
            }
        }
        int swaps =  countXy / 2 + countYx / 2;
        int mod1 = countXy % 2;
        int mod2 = countYx % 2;
        if (mod1 == mod2) {
            swaps += mod1 * 2;
        } else {
            return -1;
        }
        return swaps;
    }
}
